#include "employee.h"
#include <stdio.h>
#include <string.h>
#include "fileio.h"

#define MAX_EMPLOYEES 100

Employee employees[MAX_EMPLOYEES];
int current_employee_count = 0;

void addemployee() {
    if (current_employee_count >= MAX_EMPLOYEES) {
        printf("员工数量已达上限！\n");
        return;
    }
    Employee new_employee;
    printf("请输入员工工号：");
    scanf("%d", &new_employee.id);
    printf("请输入员工姓名：");
    scanf("%s", new_employee.name);
    printf("请输入员工基本工资：");
    scanf("%f", &new_employee.base_salary);
    printf("请输入员工奖金：");
    scanf("%f", &new_employee.bonus);
    new_employee.total_salary = new_employee.base_salary + new_employee.bonus;

    employees[current_employee_count] = new_employee;
    current_employee_count++;

    printf("员工信息已成功录入！\n");
    savetofile();  // 保存数据到文件
}

void searchsalary() {
    int search_id;
    char search_name[50];
    int search_option;
    printf("1. 通过工号查询\n2. 通过姓名查询\n请选择查询方式：");
    scanf("%d", &search_option);

    int found = 0;
    if (search_option == 1) {
        printf("请输入员工工号：");
        scanf("%d", &search_id);
        for (int i = 0; i < current_employee_count; i++) {
            if (employees[i].id == search_id) {
                printf("姓名：%s, 基本工资：%f, 奖金：%f, 总薪资：%f\n",
                    employees[i].name, employees[i].base_salary, employees[i].bonus, employees[i].total_salary);
                found = 1;
                break;
            }
        }
    } else if (search_option == 2) {
        printf("请输入员工姓名：");
        scanf("%s", search_name);
        for (int i = 0; i < current_employee_count; i++) {
            if (strcmp(employees[i].name, search_name) == 0) {
                printf("工号：%d, 基本工资：%f, 奖金：%f, 总薪资：%f\n",
                    employees[i].id, employees[i].base_salary, employees[i].bonus, employees[i].total_salary);
                found = 1;
                break;
            }
        }
    }

    if (!found) {
        printf("未找到相关员工信息。\n");
    }
}

void modifysalary() {
    int modify_id;
    printf("请输入需要修改的员工工号：");
    scanf("%d", &modify_id);
    int found = 0;
    for (int i = 0; i < current_employee_count; i++) {
        if (employees[i].id == modify_id) {
            printf("当前工资：%f, 当前奖金：%f\n", employees[i].base_salary, employees[i].bonus);
            printf("请输入新的基本工资：");
            scanf("%f", &employees[i].base_salary);
            printf("请输入新的奖金：");
            scanf("%f", &employees[i].bonus);
            employees[i].total_salary = employees[i].base_salary + employees[i].bonus;
            printf("修改成功！\n");
            savetofile();  // 保存数据到文件
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("未找到该工号的员工。\n");
    }
}

void deleteemployee() {
    int delete_id;
    printf("请输入需要删除的员工工号：");
    scanf("%d", &delete_id);
    int found = 0;
    for (int i = 0; i < current_employee_count; i++) {
        if (employees[i].id == delete_id) {
            for (int j = i; j < current_employee_count - 1; j++) {
                employees[j] = employees[j+1];
            }
            current_employee_count--;
            printf("删除成功！\n");
            savetofile();  // 保存数据到文件
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("未找到该工号的员工。\n");
    }
}

void displayall() {
    printf("工号\t姓名\t基本工资\t奖金\t总薪资\n");
    printf("---------------------------------------------------\n");
    for (int i = 0; i < current_employee_count; i++) {
        printf("%d\t%s\t%f\t%f\t%f\n",
            employees[i].id, employees[i].name, employees[i].base_salary, employees[i].bonus, employees[i].total_salary);
    }
}
